package com.example.springtransactional.service;

import com.example.springtransactional.dao.InsertDataDao;
import com.example.springtransactional.domain.User;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @auther:lgb
 * @Date: 2021/4/24
 */
@Service
public class InsertService {
    @Resource
    InsertDataDao insertDataDao;


    public void insertData(User user) {
        insertDataDao.insertData(user);
    }

    @Transactional(rollbackFor = {Exception.class}, isolation = Isolation.DEFAULT, timeout = 1)
    public User testTransactional()  {
        insertDataDao.insertData(new User(1, "xiaolu", 20));
        insertDataDao.insertData(new User(1, "xiaolu", 30));
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        insertDataDao.insertData(new User(1, "xiaolu", 40));
        insertDataDao.insertData(new User(1, "xiaolu", 50));
        insertDataDao.insertData(new User(1, "xiaolu", 60));

        return new User(1, "测试", 22);
    }
}
